package cn.com.utt.unms.mapper;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.com.utt.unms.pojo.Wireless;

@Mapper
public interface WirelessMapper {
	
	@Insert("insert into wireless(user_id,name) values(#{userId},#{name})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void save(Wireless wireless);
	
	@Update("update wireless set name=#{name} where id=#{id}")
	void update(Wireless wireless);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM wireless",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='wirelessIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void delete(@Param("wirelessIds")Collection<Long> wirelessIds);
	
	@Select("select * from wireless where id=#{wirelessId}")
	Wireless findById(@Param("wirelessId")long wirelessId);
	
	@Select({"<script>",
	    "SELECT w.*", 
	    "FROM wireless w,wireless_network nw",
	    "WHERE nw.wireless_id = w.id and ", 
	    "nw.network_id IN", 
	      "<foreach item='item' index='index' collection='networkIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Wireless> queryByNetworkIds(@Param("networkIds")Collection<Long> networkIds);
	
	@Select("SELECT w.* FROM wireless w,wireless_network nw WHERE nw.wireless_id = w.id and nw.network_id=#{networkId}")
	List<Wireless> queryByNetworkId(@Param("networkId")long networkId);
	
	@Select("SELECT * FROM wireless WHERE user_id = #{userId}")
	List<Wireless> queryByUserId(@Param("userId")long userId);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM wireless ",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='wirelessIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Wireless> queryByIds(@Param("wirelessIds")Collection<Long> wirelessIds);
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM wireless",
	    "WHERE ",
	    "user_id = #{userId} ", 
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM wireless",
	    "WHERE ",
	    "user_id = #{userId} ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<Wireless> queryByCondition(Map<String, Object> conditions);
	
}
